package com.gph.saviorframework.mq.api.bean;

import com.gph.saviorframework.mq.api.MQFactory;
import com.gph.saviorframework.mq.api.Message;
import com.gph.saviorframework.mq.api.SendResult;
import com.gph.saviorframework.mq.api.exception.MQClientException;
import com.gph.saviorframework.mq.api.transaction.LocalTransactionChecker;
import com.gph.saviorframework.mq.api.transaction.LocalTransactionExecuter;
import com.gph.saviorframework.mq.api.transaction.TransactionProducer;

import java.util.Properties;

public class TransactionProducerBean implements TransactionProducer {

    private Properties properties;
    private LocalTransactionChecker localTransactionChecker;

    private TransactionProducer transactionProducer;

    public void start() {
        if (null == this.properties) {
            throw new MQClientException("properties not set");
        }

        this.transactionProducer = MQFactory.createTransactionProducer(properties, localTransactionChecker);
        this.transactionProducer.start();
    }

    public void shutdown() {
        if (this.transactionProducer != null) {
            this.transactionProducer.shutdown();
        }
    }

    public SendResult send(Message message, LocalTransactionExecuter executer, Object arg) {
        return this.transactionProducer.send(message, executer, arg);
    }

    public Properties getProperties() {
        return properties;
    }


    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public LocalTransactionChecker getLocalTransactionChecker() {
        return localTransactionChecker;
    }

    public void setLocalTransactionChecker(LocalTransactionChecker localTransactionChecker) {
        this.localTransactionChecker = localTransactionChecker;
    }

    public boolean isStarted() {
        return this.transactionProducer.isStarted();
    }

    public boolean isClosed() {
        return this.transactionProducer.isClosed();
    }
}